package cn.edu.jlu.community.app.home;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.edu.jlu.community.R;
import cn.edu.jlu.community.app.MainActivity;
import cn.edu.jlu.community.app.other.UnlockActivity;
import cn.edu.jlu.community.server.AppServer;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {

    private MainActivity mContext;
    private FragmentManager mFragmentManager;

    PostGalleryFragment mForumPostGalleryFragment;
    PostGalleryFragment mNewsPostGalleryFragment;
    PostGalleryFragment mNotifyPostGalleryFragment;


    @InjectView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout swipeRefreshLayout;

    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        return fragment;
    }

    public HomeFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_fragment, container, false);
        ButterKnife.inject(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mContext = (MainActivity) getActivity();
        mFragmentManager = getActivity().getSupportFragmentManager();

        mForumPostGalleryFragment = PostGalleryFragment.newInstance(
                AppServer.FORUM_CATEGORY, getString(R.string.title_forum), new PostGalleryFragment.Target() {
                    @Override
                    public void run() {
                        mContext.setSectionById(R.id.forum_card);
                    }
                });

        mNotifyPostGalleryFragment = PostGalleryFragment.newInstance(
                AppServer.NOTIFICATION_CATEGORY, getString(R.string.title_notification), new PostGalleryFragment.Target() {
                    @Override
                    public void run() {
                        mContext.setSectionById(R.id.notification_card);
                    }
                });

        mNewsPostGalleryFragment = PostGalleryFragment.newInstance(
                AppServer.NEWS_CATEGORY, getString(R.string.title_news), new PostGalleryFragment.Target() {
                    @Override
                    public void run() {
                        mContext.setSectionById(R.id.news_card);
                    }
                });

        mFragmentManager.beginTransaction()
                .add(R.id.home_container_layout, mForumPostGalleryFragment)
                .add(R.id.home_container_layout, mNotifyPostGalleryFragment)
                .add(R.id.home_container_layout, mNewsPostGalleryFragment)
                .commit();

        swipeRefreshLayout.setOnRefreshListener(this);
    }


    @Override
    public void onRefresh() {
        mForumPostGalleryFragment.onRefresh();
        mNewsPostGalleryFragment.onRefresh();
        mNotifyPostGalleryFragment.onRefresh();
    }

    public SwipeRefreshLayout getSwipeRefreshLayout(){return swipeRefreshLayout;}

    @OnClick(R.id.unlock_button)
    public void onUnlockButtonClick() {
        mContext.startActivity(new Intent(mContext, UnlockActivity.class));
    }
}
